﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Bank
{
    public partial class Login : Form
    {
        public static Login logForm;    //标识登陆界面
        public static Choice chooseForm;   //标识业务选择界面
        private Bank bank;
        private int loginAcc;   //标识登录账号

        public Login()
        {
            InitializeComponent();
            logForm = this;
        }

        private void Login_Load(object sender, EventArgs e)
        {
            //初始化数据
            bank = new Bank();
            bank[0] = new CreditAccount(6543210, 1000, 123456, 10000, 10000, DateTime.Now.AddMonths(1));
            bank[1] = new CreditAccount(7654321, 666, 001728, 50000, 50000, DateTime.Now.AddMonths(1));
            bank[2] = new CreditAccount(8765432, 2333, 654321, 1000, 1000, DateTime.Now.AddMonths(1));
        }

        public int getLoginAcc()
        {
            return loginAcc;
        }

        public Bank getBank()
        {
            return bank;
        }

        //非法输入检验
        private bool IsLegal(string a)
        {
            //字符串为空则直接返回false
            if (a == "")
                return false;
            Regex rx = new Regex("^[0-9]*$");
            return rx.IsMatch(a);
        }

        //登录
        private void log_Click(object sender, EventArgs e)
        {
            //非法输入检验
            if(!IsLegal(name.Text) || !IsLegal(pwd.Text))
            {
                MessageBox.Show("非法输入，请重新输入", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //验证账户是否存在
            if(bank.findCreditAccount(int.Parse(name.Text)) == null)
            {
                MessageBox.Show("未查找到账户，请重新输入", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            int rPwd = bank.findCreditAccount(int.Parse(name.Text)).getPwd();
            if(int.Parse(pwd.Text) != rPwd)
            {
                MessageBox.Show("账户号或密码错误，请重新输入", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                loginAcc = int.Parse(name.Text);    //设置当前登录账号
                this.Hide();
                chooseForm = new Choice();   //显示事务选择界面
                chooseForm.Show();
            }
        }
    }
}
